function studentSearch_clicked()
{
	var studentNumber=$("#studentNumber").val();
	$.ajax({
            type: 'Post',
            url: '/addrecord_action_searchStudent',
            data: "studentNumber="+studentNumber,
			dataType:"json",
            success: function(response) {
                searchStudentSucceed(response);
            }
        })
}
function searchStudentSucceed(data)
{
	$('tr[id="message"]').css("display","none");
	$('tr[id="nameAndMail"]').css("display","none");
	
	if(data.length==0)
	{
		$('tr[id="message"]').css("display","block");
		$("#div_message").html("找不到此学号学生");
	}
	else
	{
		$('tr[id="nameAndMail"]').css("display","block");
		$('tr[id="nameAndMail"]').find('#divName').html(data[0].studentName);
		$('tr[id="nameAndMail"]').find('#divMail').html(data[0].studentMail);
	}
}

function examSearch_clicked()
{
	var courseName=$('#courseName').val();
	var examName=$("#examName").val();
	$.ajax({
            type: 'Post',
            url: '/addrecord_action_searchExam',
            data: "examName="+examName+"&courseName="+courseName,
			dataType:"json",
            success: function(response) {
                searchExamSucceed(response);
            }
        })
}
function  searchExamSucceed(data)
{
	$('tr[id="message2"]').css("display","none");
	$('tr[id="nameAndCourse"]').css("display","none");
	$('tr[id="nameAndCourseHead"]').css("display","none");
	$("#table_form").addClass("hideTd");
	if(data.length==0)
	{
		$('tr[id="message2"]').css("display","block");
		$("#div_message2").html("找不到考试");
		$("#submit_records").addClass("hide");
	}
	else
	{
		$('tr[id="nameAndCourse"]').css("display","block");
		$('tr[id="nameAndCourseHead"]').css("display","block");
		$('tr[id="nameAndCourse"]').find('#divExamName').html(data[0].examName);
		var courseName=data[0].courseName;
		var filter='option[value="'+courseName+'"]';
		var courseNameChinese=$("#courseName").find(filter).html();
		$('tr[id="nameAndCourse"]').find('#divExamCouse').html(courseNameChinese);
		//alert($('tr[id="nameAndCourse"]').find('#divExamCousePYin').html())
		$('tr[id="nameAndCourse"]').find('#divExamCousePYin').html(courseName);
		if($("#table_form").find('tr[id="ready"]').length==0)
		{
			getAllStudent();
		}
		else
		{
			$("#table_form").removeClass("hideTd");
		}
	}
}

function getAllStudent()
{
	$.ajax({
            type: 'Post',
            url: '/common_action_getAllStudent',
			dataType:"json",
            success: function(response) {
				buildUpRegTable(response);
            }
        })
}

function buildUpRegTable(data)
{
	var tableForm=$("#table_form");
	$(tableForm).removeClass("hideTd");
	$(data).each(function(){
	    var row = $("#table_form").find('#template').clone();
		$(row).removeClass("hideTd");
		$(row).find("#studentName").find("div").html(this.studentName);
		$(row).find("#studentNumber").find("div").html(this.studentNumber);
		$(row).attr("id","ready");
		row.appendTo(tableForm);
	})
	$("#submit_records").removeClass("hide");
}

function saveRecords_clicked()
{
	var needPost=true;
	var tableForm=$("#table_form");
	var trLists=$(tableForm).find('tr[id="ready"]');
	$("#div_messageSaveResult").html("");
	var examName=$('#divExamName').html();
	var courseName=$('#divExamCousePYin').html();
	
	var saving_json	='{"exam":"'+examName+'","course":"'+courseName+'","records":';
	saving_json+='[';
				  
	$(trLists).each(function(){
						var studentName=$(this).find('td[id="studentName"]').find('div').html();
						//alert(studentName);
						var studentNumber=$(this).find('td[id="studentNumber"]').find('div').html();
						//alert(studentNumber);
						var studentRecord=$(this).find('td[id="studentRecord"]').find('input').val();
						//alert(studentRecord);
						var validnumber=CheckNumber(studentRecord);
						if(validnumber==false)
						{
							var errorMsg='*学生：'+studentName+' 的成绩不正确';
							$("#div_messageSaveResult").html('*学生：'+studentName+' 的成绩不正确');
							needPost=false;
							return;
						}//end of if
						saving_json+='{"studentNumber":"'+studentNumber+'","studentRecord":"'+studentRecord+'"},'
							})//end of each
	
	if(needPost==true)
	{
		saving_json=saving_json.substring(0,saving_json.length-1);
		saving_json+=']}'
		//alert(saving_json);
		postRecords(saving_json);
	}
}

function postRecords(json)
{
	var examName=$('#examName').val();
	$.ajax({
            type: 'Post',
            url: '/addrecord_action_addExamRecordList',
            data: "record_json="+json,
            success: function(response) {
                alert(response);
            }
        })
}

function CheckNumber(num)
{
	var patrn=/^[0-9]*[1-9][0-9]*$/;
	if   (!patrn.exec(num))     
	  return false;  
	else 
	  return true;  
}